/*
 * chardev.c: Creates a read-only char device that says how many times
 * you have read from the dev file
 */

#include <linux/atomic.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>	/* for sprintf() */
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/types.h>
#include <linux/version.h>
#include <asm/errno.h>
#include <linux/completion.h>

#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
enum {
    CDEV_NOT_USED,
    CDEV_EXCLUSIVE_OPEN,
};

/* Is device open? Used to prevent multiple access to device */
static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
static char data[16];
static int data_len = sizeof(data);
static struct completion read_completion;
static struct class* cls;

static int device_open(struct inode* inode, struct file* file)
{
    static int counter = 0;

    if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
        return -EBUSY;

    printk("I already told you %d times Hello world!\n", counter++);
    try_module_get(THIS_MODULE);

    return SUCCESS;
}

static int device_release(struct inode* inode, struct file* file)
{
    atomic_set(&already_open, CDEV_NOT_USED);
    module_put(THIS_MODULE);

    return SUCCESS;
}

static ssize_t device_read(struct file* filp,
    char __user* buffer, size_t length, loff_t* offset)
{
    int ret = 0;
    int len = length;
    int real_data_len;
    if (*offset != 0) {
        return 0;
    }

    pr_info("read statrt\n");
    wait_for_completion(&read_completion);
    pr_info("read completion done\n");

    real_data_len = strlen(data);
    if (length > real_data_len)
        len = real_data_len;
    ret = copy_to_user(buffer, data, len);
    if (ret == 0) {
        ret = len;
    }
    *offset = len;
    pr_info("read ret=%d, data=%s\n", ret, data);
    memset(data, 0, data_len);

    return ret;
}

static ssize_t device_write(struct file* filp, const char __user* buff,
    size_t len, loff_t* off)
{

    return 0;
}

static struct file_operations chardev_fops = {
    .read = device_read,
    .write = device_write,
    .open = device_open,
    .release = device_release,
};

// sysfs
static ssize_t data_store(struct kobject* kobj,
    struct kobj_attribute* attr, const char* buf,
    size_t count)
{
    int buf_len = strlen(buf);
    if (buf_len >= data_len)
        return -1;

    pr_info("sysfs data_sotre count=%ld\n", count);
    sscanf(buf, "%s", data);
    complete(&read_completion);
    return count;
}

static struct kobj_attribute data_attribute =
__ATTR(data, 0664, NULL, data_store);

static struct kobject* example_kobj;

static int data_sysfs_init(void)
{
    int error = 0;

    pr_info("sysfs_init\n");

    example_kobj = kobject_create_and_add("example", kernel_kobj);
    if (!example_kobj)
        return -ENOMEM;

    error = sysfs_create_file(example_kobj, &data_attribute.attr);
    if (error) {
        kobject_put(example_kobj);
        pr_info("failed to create the myvariable file "
            "in /sys/kernel/example\n");
    }

    return error;
}

static void data_sysfs_exit(void)
{
    pr_info("example: Exit success\n");
    kobject_put(example_kobj);
}

static int __init chardev_init(void)
{
    major = register_chrdev(0, DEVICE_NAME, &chardev_fops);

    if (major < 0) {
        pr_alert("Registering char device failed with %d\n", major);
        return major;
    }

    pr_info("I was assigned major number %d.\n", major);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
    cls = class_create(DEVICE_NAME);
#else
    cls = class_create(THIS_MODULE, DEVICE_NAME);
#endif
    device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
    init_completion(&read_completion);

    data_sysfs_init();
    pr_info("Device created on /dev/%s\n", DEVICE_NAME);

    return SUCCESS;
}

static void __exit chardev_exit(void)
{
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);

    /* Unregister the device */
    unregister_chrdev(major, DEVICE_NAME);

    data_sysfs_exit();
}

module_init(chardev_init);
module_exit(chardev_exit);

MODULE_LICENSE("GPL");
